//
//  CYAppealTVC.m
//  CreditAddressBook
//
//  Created by LE on 16/1/6.
//  Copyright © 2016年 Lee. All rights reserved.
//

#import "CYAppealTVC.h"
#import "LEImagePicker.h"
#import "CYCreditTool.h"
#import "MBProgressHUD+MJ.h"
#import "LEProgressButton.h"
#import "LEAlertView+LE.h"
#import "CYLoginHttpTool.h"
#import "CYJumpsTool.h"

@interface CYAppealTVC ()

@property (weak, nonatomic) IBOutlet UITextField *nameTF;
@property (weak, nonatomic) IBOutlet UITextView *reasonTV;
@property (weak, nonatomic) IBOutlet UITextField *phoneTF;
@property (weak, nonatomic) IBOutlet UIButton *exampleBtn;

@property(nonatomic, strong) LEImagePicker *imagePicker;
@property(nonatomic, strong) NSString *certificateNo;
@property(nonatomic, strong) NSString *certificateNoZ;
@property(nonatomic, strong) NSString *certificateNoF;
@end

@implementation CYAppealTVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupAttribute];
}

- (void)setupAttribute {
    //申诉原因边框
    self.reasonTV.layer.borderWidth = .5;
    self.reasonTV.layer.borderColor = [UIColor grayColor].CGColor;
    //照片选择器
    self.imagePicker = [[LEImagePicker alloc] init];
    //示例按钮边框
    self.exampleBtn.layer.borderWidth = 1;
    self.exampleBtn.layer.masksToBounds = YES;
    self.exampleBtn.layer.borderColor = CYNaviThemeColor.CGColor;
}
/**
 *  提交
 */
- (void)submitBtn {
    NSDictionary *param = [self paramDic];
    if (param) {
        [MBProgressHUD showMessage:nil toView:self.view];
        [CYCreditTool saveAllegeWithParams:param success:^(NSString *message) {
            [MBProgressHUD hideHUDForView:self.view animated:NO];
            [MBProgressHUD showSuccess:message];
        } failure:^(NSString *message, BOOL isLogin) {
            [MBProgressHUD hideHUDForView:self.view animated:NO];
            if (isLogin) {
                [MBProgressHUD showError:message];
            } else {
                [MBProgressHUD showError:kCYLoginStatusError];
                [self.navigationController popViewControllerAnimated:NO];
                [CYLoginHttpTool logoutWithController:self];
            }
        }];
    }
}
- (NSDictionary *)paramDic {
    CYUser *user = [CYLoginHttpTool userData].user;
    if (user) {
        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
        dic[@"userId"] = user.userId;
        dic[@"token"] = user.token;
        if (self.defrId && !self.defrId.isEmpty) {
            dic[@"id"] = self.defrId;
        } else {
            [MBProgressHUD showError:@"申诉记录异常"];
            return nil;
        }
        dic[@"type"] = @"2";
        if (self.certificateNo) {
            dic[@"certificateNo"] = self.certificateNo;
        } else {
            [MBProgressHUD showError:@"请上传手持身份证照"];
            return nil;
        }
        if (self.certificateNoZ) {
            dic[@"certificateNoZ"] = self.certificateNoZ;
        } else {
            [MBProgressHUD showError:@"请上传身份证正面照"];
            return nil;
        }
        if (self.certificateNoF) {
            dic[@"certificateNoF"] = self.certificateNoF;
        } else {
            [MBProgressHUD showError:@"请上传身份证反面照"];
            return nil;
        }
        if (self.nameTF.text && !self.nameTF.text.isEmpty) {
            dic[@"name"] = self.nameTF.text;
        } else {
            [MBProgressHUD showError:@"输入姓名"];
            return nil;
        }
        if (self.reasonTV.text && !self.reasonTV.text.isEmpty) {
            dic[@"allegeRecord"] = self.reasonTV.text;
        } else {
            [MBProgressHUD showError:@"请输入申诉原因"];
            return nil;
        }
        if (self.phoneTF.text && !self.phoneTF.text.isEmpty) {
            dic[@"linkPhone"] = self.phoneTF.text;
        } else {
            [MBProgressHUD showError:@"请输入联系电话"];
            return nil;
        }
        return dic;
    } else {
        [MBProgressHUD showError:@"用户没有登录"];
        [self.navigationController popViewControllerAnimated:YES];
        return nil;
    }
}
/**
 *  示例
 */
- (IBAction)exampleBtn:(UIButton *)sender {
    [self.view.window endEditing:YES];
    UIImageView *shareView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"idcard_consult.jpg"]];
//    CGFloat ratio = shareView.height / shareView.width;
    //    CGFloat height = CYScreeSize.height - 130;
//    CustomIOSAlertView *alertView = [[CustomIOSAlertView alloc] init];
//    CGFloat w = CYScreeSize.width - 32;
//    shareView.frame = CGRectMake(0, 0, w, w * ratio);
//    alertView.containerView = shareView;
//    alertView.buttonTitles = @[@"知道了"];
    [LEAlertView showAlertCustomViewWithButton:nil cancelButton:@"知道了" customView:shareView clickAtIndex:^(NSInteger index) {
        
    }];
}
/**
 *  手持身份证
 */
- (IBAction)certificateNoBtn:(LEProgressButton *)sender {
    [self.view.window endEditing:YES];
    CYWeakObject(weakself, self);
    [self.imagePicker showInController:self handler:^(UIImage *image) {
        [CYCreditTool uploadUserImage:image button:sender success:^(NSString *imagePath) {
            weakself.certificateNo = imagePath;
        } failure:^(NSString *message, NSError *error) {
            [MBProgressHUD showError:message];
        }];
    }];
}
/**
 *  身份证正面
 */
- (IBAction)certificateNoZBtn:(LEProgressButton *)sender {
    [self.view.window endEditing:YES];
    CYWeakObject(weakself, self);
    [self.imagePicker showInController:self handler:^(UIImage *image) {
        [CYCreditTool uploadUserImage:image button:sender success:^(NSString *imagePath) {
            weakself.certificateNoZ = imagePath;
        } failure:^(NSString *message, NSError *error) {
            [MBProgressHUD showError:message];
        }];
    }];
    
}
/**
 *  身份证反面
 */
- (IBAction)certificateNoFBtn:(LEProgressButton *)sender {
    [self.view.window endEditing:YES];
    CYWeakObject(weakself, self);
    [self.imagePicker showInController:self handler:^(UIImage *image) {
        [CYCreditTool uploadUserImage:image button:sender success:^(NSString *imagePath) {
            weakself.certificateNoF = imagePath;
        } failure:^(NSString *message, NSError *error) {
            [MBProgressHUD showError:message];
        }];
    }];
}

#pragma mark - Table view data source
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return .1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 10;
}
@end
